001    /* $RCSfile: DES1KeySecretKeyGeneratorEngine.java,v $
002     * $Revision: 1.10 $
003     * $Date: 2003/10/04 19:18:38 $
004     * $Author: uwe_guenther $
005     * $State: Exp $
006     *
007     * Created on August 13, 2001 2:51 PM
008     *
009     * Copyright (C) 2001 Uwe Guenther <uwe@cscc.de>
010     *
011     * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
012     * ServiceProvider is a library, written in JavaTM, that should be 
013     * used in HBCI banking applications (clients and may be servers),
014     * to do cryptographic operations.
015     *
016     * The jhbci library is free software; you can redistribute it and/or
017     * modify it under the terms of the GNU Lesser General Public
018     * License as published by the Free Software Foundation; either
019     * version 2.1 of the License, or (at your option) any later version.
020     *
021     * The jhbci library is distributed in the hope that it will be useful,
022     * but WITHOUT ANY WARRANTY; without even the implied warranty of
023     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
024     * Lesser General Public License for more details.
025     *
026     * You should have received a copy of the GNU Lesser General Public
027     * License along with this library; if not, write to the Free Software
028     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029     *
030     */
031    
032    package de.cscc.crypto.provider;
033    
034    import java.security.InvalidAlgorithmParameterException;
035    import java.security.InvalidParameterException;
036    import java.security.SecureRandom;
037    import java.security.spec.AlgorithmParameterSpec;
038    
039    import javax.crypto.KeyGeneratorSpi;
040    import javax.crypto.SecretKey;
041    
042    /**
043     * DES1KeySecretKeyGeneratorEngine Class provides the functionality
044     * of a DESede key generator for two keys (means 56 or 64 bit key length).
045     * This key generator generates only keys for the "DES1Key" algorithm in
046     * the JHBCI crypto provider.
047     *
048     * This KeyGenerator Object is re-useable, i.e., after a key has been generated,
049     * the same KeyGenerator Object  can be re-used to generate further keys.
050     *
051     * <pre>
052     *
053     * Use the DES1KeySecretKeyGeneratorEngine through the JCE in the following way:
054     *
055     * import javax.crypto.KeyGenerator;
056     * import javax.crypto.SecretKey;
057     * import java.security.SecureRandom;
058     *
059     * KeyGenerator kg = new KeyGenerator.getInstance("DES1Key", "JHBCI");
060     * kg.init(new SecureRandom());
061     * SecretKey key = kg.generateKey();
062     *
063     * //Use key further in a DES1Key Cipher from the JHBCI provider.
064     * //Or do some other useful things with this nicely generated SecretKey.
065     *
066     * </pre>
067     *
068     *
069     * In this case you does not explicitly initialize the KeyGenerator Object
070     * (in our example code that means <code>kg.init(new SecureRandom());</code>)
071     * we initialize the KeyGenerator Object with <code>new SecureRandom()</code>.
072     * So you can omit the line <code>kg.init(new SecureRandom());</code>.
073     * With this code we will get a SecureRandom implementation of the
074     * highest-priority installed provider. If no one of the installed
075     * providers supply an implementation of SecureRandom, a system provided
076     * source of randomness will be used.
077     * <pre>
078     *
079     * See the changed example:
080     *
081     * import javax.crypto.KeyGenerator;
082     * import javax.crypto.SecretKey;
083     * import java.security.SecureRandom;
084     *
085     * KeyGenerator kg = new KeyGenerator.getInstance("DES1Key", "JHBCI");
086     * SecretKey key = kg.generateKey();
087     *
088     * //Use key further in a DES1Key Cipher from the JHBCI provider.
089     * //Or do some other useful things with this nicely generated SecretKey.
090     *
091     * </pre>
092     * @author  <a href=mailto:uwe@cscc.de>Uwe Günther</a>
093     * @version $Revision: 1.10 $
094     */
095    public final class DES1KeySecretKeyGeneratorEngine extends KeyGeneratorSpi {
096    
097        /** The delegate for this wrapper object */
098        private DES1KeySecretKeyGeneratorImpl generator = 
099        new DES1KeySecretKeyGeneratorImpl();
100        
101        /** 
102         * Creates new DES1KeySecretKeyGeneratorEngine.
103         * Mostly used of the static function
104         * <code>KeyGenerator.getInstance(...)</code>.
105         *
106         * @throws SecurityException if the provider self integrity check fails.
107         */
108        public DES1KeySecretKeyGeneratorEngine() {
109            if (JHBCI.selfIntegrityChecking() == false) {
110                throw new SecurityException("JHBCI-Provider is tampered.");
111            }         
112        }
113        
114        /** 
115         * Returns a string representation of the object.
116         *
117         * @return a string representation of the object.
118         */
119        public String toString() {
120            return this.generator.toString();
121        }
122        
123        /** 
124         * Initializes the key generator.
125         *
126         * @param random the source of randomness for this generator
127         */
128        protected void engineInit(SecureRandom random) {
129            this.generator.init(random);
130        }
131        
132        /** 
133         * Initializes this key generator for a certain keysize, using the given
134         * source of randomness. We support only 56 bit and 64 bit keysize,
135         * which means the refers to the same result. 
136         *
137         * @param keysize the keysize. This is an algorithm-specific metric,
138         * specified in number of bits.
139         * @param random the source of randomness for this key generator.
140         * @throws InvalidParameterException if keysize is wrong or not supported.
141         */
142        protected void engineInit(int keysize, SecureRandom random) 
143                throws InvalidParameterException {
144            this.generator.init(keysize, random);
145        }
146        
147        /** 
148         * Initializes the key generator with the specified parameter
149         * set and a user-provided source of randomness. The JHBCI
150         * key generator implementation don't use any AlgorithmParameterSpec's.
151         * This means if you want to init the KeyGenerator Object with this
152         * method <code>params</code> have to be null or
153         * InvalidAlgorithmParameterException will be thrown.
154         *
155         * @param params the key generation parameters
156         * @param random the source of randomness for this key generator.
157         * @exception InvalidAlgorithmParameterException if <code>params</code> is
158         *             inappropriate for this key generator. We only support 
159         *             <CODE>null</CODE> as parameter for <CODE>params</CODE>.
160         */
161        protected void engineInit(AlgorithmParameterSpec params, SecureRandom random)
162                throws InvalidAlgorithmParameterException {
163            this.generator.init(params, random);
164        }
165        
166        /** 
167         * Generates a secret key. This method can re-used more than once
168         * for the KeyGenerator Object to generate a lot of SecretKey's.
169         *
170         * @return the new key.
171         */
172        protected SecretKey engineGenerateKey() {
173            return this.generator.generateKey();
174        }
175        
176    }